﻿using DKPScoreWebApi.DTOs.Requests;
using DKPScoreWebApi.DTOs.Results;
using DKPScoreWebApi.Models;
using DKPScoreWebApi.Services.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DKPScoreWebApi.Controllers
{
    [ApiController]
    [Route("api/guilds/{guildId}/items")]
    public class ItemsController : ControllerBase
    {
        private readonly IDkpService _dkpService;
        private readonly IGuildService _guildService;
        private readonly ILogger<ItemsController> _logger;

        public ItemsController(IDkpService dkpService, IGuildService guildService, ILogger<ItemsController> logger)
        {
            _dkpService = dkpService;
            _guildService = guildService;
            _logger = logger;
        }

        [HttpGet]
        public async Task<ActionResult<ApiResult<List<PointItem>>>> GetItems(int userId, int guildId)
        {
            try
            {
                if (!await _guildService.IsUserGuildAdminAsync(userId, guildId))
                    return Unauthorized(new ApiResult { Success = false, Message = "无权限访问此工会" });

                var items = await _dkpService.GetGuildPointItemsAsync(guildId);
                return Ok(new ApiResult<List<PointItem>> { Success = true, Data = items });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取积分项目失败");
                return BadRequest(new ApiResult { Success = false, Message = "获取积分项目失败" });
            }
        }

        [HttpPost]
        public async Task<ActionResult<ApiResult<PointItem>>> CreateItem(int userId, int guildId, [FromBody] CreateItemRequest request)
        {
            try
            {
                if (!await _guildService.IsUserGuildAdminAsync(userId, guildId))
                    return Unauthorized(new ApiResult { Success = false, Message = "无权限操作此工会" });

                var item = await _dkpService.CreatePointItemAsync(guildId, request);
                return Ok(new ApiResult<PointItem> { Success = true, Data = item });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建积分项目失败");
                return BadRequest(new ApiResult { Success = false, Message = "创建积分项目失败" });
            }
        }

        [HttpPut("{itemId}")]
        public async Task<ActionResult<ApiResult>> UpdateItem(int userId, int guildId, int itemId, [FromBody] CreateItemRequest request)
        {
            try
            {
                if (!await _guildService.IsUserGuildAdminAsync(userId, guildId))
                    return Unauthorized(new ApiResult { Success = false, Message = "无权限操作此工会" });

                var success = await _dkpService.UpdatePointItemAsync(itemId, request);
                if (!success)
                    return NotFound(new ApiResult { Success = false, Message = "项目不存在" });

                return Ok(new ApiResult { Success = true, Message = "更新成功" });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新积分项目失败");
                return BadRequest(new ApiResult { Success = false, Message = "更新积分项目失败" });
            }
        }

        [HttpDelete("{itemId}")]
        public async Task<ActionResult<ApiResult>> DeleteItem(int userId, int guildId, int itemId)
        {
            try
            {
                if (!await _guildService.IsUserGuildAdminAsync(userId, guildId))
                    return Unauthorized(new ApiResult { Success = false, Message = "无权限操作此工会" });

                var success = await _dkpService.DeletePointItemAsync(itemId);
                if (!success)
                    return NotFound(new ApiResult { Success = false, Message = "项目不存在" });

                return Ok(new ApiResult { Success = true, Message = "删除成功" });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除积分项目失败");
                return BadRequest(new ApiResult { Success = false, Message = "删除积分项目失败" });
            }
        }

        [HttpGet("{itemId}")]
        public async Task<ActionResult<ApiResult<PointItem>>> GetItem(int userId, int guildId, int itemId)
        {
            try
            {
                if (!await _guildService.IsUserGuildAdminAsync(userId, guildId))
                    return Unauthorized(new ApiResult { Success = false, Message = "无权限访问此工会" });

                var item = await _dkpService.GetPointItemByIdAsync(itemId);
                if (item == null || item.GuildId != guildId)
                    return NotFound(new ApiResult { Success = false, Message = "项目不存在" });

                return Ok(new ApiResult<PointItem> { Success = true, Data = item });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取积分项目详情失败");
                return BadRequest(new ApiResult { Success = false, Message = "获取项目详情失败" });
            }
        }
    }
}
